package com.taokatao.cardmee.business.cardcode;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taokatao.cardmee.App;
import com.taokatao.cardmee.C;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.db.model.Card;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class CardCodePreviewLayout extends ICardDetailInfoLayout implements View.OnClickListener {

    @BindView(R.id.t_scan_tips) TextView tScanTips;
    @BindView(R.id.t_ali_pay) TextView tAliPay;
    @BindView(R.id.t_wx_pay) TextView tWXPay;

    @BindView(R.id.l_barcode) View lBarcode;
    @BindView(R.id.i_value) ImageView iValue;
    @BindView(R.id.t_value) TextView tValue;

    @BindView(R.id.i_code) ImageView iCode;

    @BindView(R.id.l_user_info) View lUser;
    @BindView(R.id.i_icon) ImageView iIcon;
    @BindView(R.id.t_username) TextView tUsername;
    @BindView(R.id.t_user_tel) TextView tUserTel;


    Card c;

    public CardCodePreviewLayout(Context context) {
        this(context, null);
    }

    public CardCodePreviewLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        ButterKnife.bind(LayoutInflater.from(context).inflate(R.layout.layout_card_code_scan, this));
        tScanTips.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG | Paint.UNDERLINE_TEXT_FLAG);
    }

    @Override
    @OnClick({R.id.t_ali_pay, R.id.t_wx_pay, R.id.t_scan_tips, R.id.i_value, R.id.i_code})
    public void onClick(View v) {
        if (v == tAliPay) {
            Utils.onMobEvent(getContext(), "clickPayOfCardDetails");
            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.eg.android.AlipayGphone", "com.eg.android.AlipayGphone.AlipayLogin"));
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getContext().startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                Utils.showToast("支付宝客户端打开失败");
            }
        } else if (v == tWXPay) {
            Utils.onMobEvent(getContext(), "clickPayOfCardDetails");
            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"));
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getContext().startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                Utils.showToast("微信客户端打开失败");
            }
        } else if (v == iValue || v == iCode) {
            if (c != null) {
                Intent intent = new Intent(getContext(), CardCodePictureActivity.class);
                intent.putExtra("extra_card", c);
                intent.putExtra("extra_select_item", "");
                getContext().startActivity(intent);
            }
        } else if (v == tScanTips) {
            Utils.onMobEvent(getContext(), "clickProblemOfScan");
            Intent intent = new Intent(getContext(), CardCodeScanTipsActivity.class);
            getContext().startActivity(intent);
        }
    }

    @Override
    void prepareData(Card card) {
        if (card == null) return;
        c = card;

        if (!TextUtils.isEmpty(c.getMCardNo())) {
            lUser.setVisibility(View.GONE);
            lBarcode.setVisibility(View.VISIBLE);

            if (TextUtils.equals("2", c.getBarcodeStatus())) {
                iValue.setVisibility(View.GONE);
                iCode.setVisibility(View.VISIBLE);
                try {
                    tValue.setVisibility(View.GONE);
                    iCode.setImageBitmap(Utils.createQRCode(c.getMCardNo()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                iCode.setVisibility(View.GONE);
                iValue.setVisibility(View.VISIBLE);
                try {
                    tValue.setVisibility(View.VISIBLE);
                    tValue.setText(Utils.formatCardCode(c.getMCardNo()));
                    iValue.setImageBitmap(Utils.createBarcode(c.getMCardNo()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            lUser.setVisibility(View.VISIBLE);
            lBarcode.setVisibility(View.GONE);

            boolean isCustomMode = C.settings.BUSINESS_OTHER_ID == c.getBusinessId();
            String iconUrl = isCustomMode ? c.getBuPic() : c.getMCardIcon();
            if (!TextUtils.isEmpty(iconUrl)) {
                Glide.with(getContext()).load(iconUrl).bitmapTransform(App.i().getCropCircleTransformation()).into(iIcon);
            }
            tUsername.setText(c.getCardUser());
            tUserTel.setText(c.getCardTel());
        }
    }
}
